package com.eonmind.user.utils;

import cn.hutool.core.util.StrUtil;
import com.eonmind.user.dto.UserDto;

public class DesensitizationUtil {
    //脱敏处理
    public  static UserDto.UserLoginResponse desensitization(UserDto.UserLoginResponse response) {
        response.setEmail(desensitizeEmail(response.getEmail()));
        response.setPhoneNumber(desensitizePhoneNumber(response.getPhoneNumber()));
        return response;
     }

    /**
     * 对邮箱进行脱敏处理
     * @param email 邮箱地址
     * @return 脱敏后的邮箱地址
     */
    public static String desensitizeEmail(String email) {
        if (StrUtil.isEmpty(email)) {
            return email;
        }
        return StrUtil.replace(email, 1, email.indexOf('@') - 1, '*');
    }

    /**
     * 对手机号进行脱敏处理
     * @param phoneNumber 手机号
     * @return 脱敏后的手机号
     */
    public static String desensitizePhoneNumber(String phoneNumber) {
        if (StrUtil.isEmpty(phoneNumber) || phoneNumber.length() < 8) {
            return phoneNumber;
        }
        return StrUtil.replace(phoneNumber, 3, phoneNumber.length() - 4, "****");
    }
}
